/*
 * @Author: your name
 * @Date: 2021-06-06 22:16:21
 * @LastEditTime: 2021-06-16 17:36:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \CPP\UDP\server.cpp
 */
#include<iostream>
using namespace std;

#include <stdio.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include "case.h"
#include "parse.h"
#include "database.h"
#include "serversocket.h"
 
 
int main(int argc,char *argv[])
{
	//判断命令行参数是否满足
	if(argc != 2)
	{
		printf("请传递一个端口号\n");
		return -1;
	}

	//创建通信对象
	Serversocket s_socket(argv[1]);
	//创建数据库对象
	Database db;
	
	//创建解析报文Parse对象
	Parse _parse;
	//获取info长度
	s_socket.send_length = sizeof(info);
	cout << "即将while" << endl;
	//3 循环接收客户发送过来的数据  
	while(1)
	{
		//清除缓存
		s_socket.clear_Cache();
		//接收信息
		s_socket.recv_From_Client();
		//获取opcode
		info* tinfo = (info*)s_socket.recv_buf;
		int opcode = tinfo->opcode;
		cout << "opcode:" << opcode << endl;
		//根据opcode进行不同的回复
		switch (opcode)
		{
			//注册
		case _REGISTER:
			cout << "注册账户" << endl;
			_parse.deal_Register(s_socket.recv_buf,s_socket.send_buf);
			break;
			//登录
		case _LOGIN:
			_parse.deal_Login(s_socket.recv_buf,s_socket.send_buf);
			break;
			//展示商品
		case _SHOW_ALL_COMMODITY:
			_parse.deal_Query_All_Commodity(s_socket.recv_buf,s_socket.send_buf);		
			break;
			//退出系统	
		case _EXIT_SYSTEM:			
			break;	
			//请求充值
		case _RECHARGE:
			_parse.deal_Recharge(s_socket.recv_buf,s_socket.send_buf);
			break;	
			//请求修改密码
		case _CHANGE_PASSWD:
			_parse.deal_Change_Passwd(s_socket.recv_buf,s_socket.send_buf);
			break;
			//请求检索商品
		case _SEARCH_COMMODITY:
			_parse.deal_Search_Commodity(s_socket.recv_buf,s_socket.send_buf);
			break;
			//给定identifier,请求该商品信息
		case _SEARCH_COM_BY_ID:
			_parse.deal_Search_Single_Commodity(s_socket.recv_buf,s_socket.send_buf);
			break;
			//请求添加购物车
		case _ADD_CART:
			_parse.deal_Add_Cart(s_socket.recv_buf,s_socket.send_buf);
			break;
			//请求修改购物车
		case _MODIFY_CART:
			_parse.deal_Modify_Cart(s_socket.recv_buf,s_socket.send_buf);
			break;
			//订单功能
		case _CREATE_ORDER:
			_parse.deal_Create_Order(s_socket.recv_buf,s_socket.send_buf);
			break;
			//添加商品
		case _ADD_COMMODITY:
			_parse.deal_Add_Commodity(s_socket.recv_buf,s_socket.send_buf);
			break;
			//修改商品
		case _MODIFY_COMMODITY:
			_parse.deal_Modify_Commodity(s_socket.recv_buf,s_socket.send_buf);
			break;
		default:
			cout << "default" << endl;
			break;
		}		
		s_socket.send_To_Client();
	}
}